function linesIntersection(x1,y1,x2,y2,x3,y3,x4,y4){
	if(!(x1!=null&&x2!=null&&y1!=null&&
		 y2!=null&&x3!=null&&x4!=null&&
		 y3!=null&&y4!=null)){
		return null;
	}
	var answer=[2];
	var a1=y1-y2;
	var b1=x2-x1;
	var c1=x1*y2-x2*y1;
	var a2=y3-y4;
	var b2=x4-x3;
	var c2=x3*y4-x4*y3;
	var s=a1*b2-a2*b1
	if(!(s)){
		return null;
	}
	answer[0]=(c2*b1-c1*b2)/s;
	answer[1]=(c1*a2-c2*a1)/s;
	return answer;
}

function segmentsIntersection(x1,y1,x2,y2,x3,y3,x4,y4){
	var answer=linesIntersection(x1,y1,x2,y2,x3,y3,x4,y4);
	if(answer==null){
		return null;
	}
	var s;
	if(x1>x2){
		s=x1;
		x1=x2;
		x2=s;
	}
	if(x3>x4){
		s=x3;
		x3=x4;
		x4=s;
	}
	if(y1>y2){
		s=y1;
		y1=y2;
		y2=s;
	}
	if(y3>y4){
		s=y3;
		y3=y4;
		y4=s;
	}
	var dx=0.001;
	x1-=dx;
	x2+=dx;
	x3-=dx;
	x4+=dx;
	y1-=dx;
	y2+=dx;
	y3-=dx;
	y4+=dx;
	if((answer[0]<=x2&&answer[0]<=x4&&answer[0]>=x1&&answer[0]>=x3)&&
		(answer[1]<=y2&&answer[1]<=y4&&answer[1]>=y1&&answer[1]>=y3)){
		return answer;
	}else{
		return null;
	}
}